Pattern Matching

A pattern match includes a sequence of alternatives, each starting with the keyword case. Each alternative includes a pattern and one or more expressions, which will be evaluated if the pattern matches. An arrow symbol => separates the pattern from the expressions.

Try the following example program, which shows how to match against an integer value.

Example:1
object demo {
def main(args: Array[String]) {
println(matchTest(10))
}
def matchTest(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case _ => "many"
}
}
many

Example:2
object demo
{
def mat (x: Int): String = x match {
case 1 => "Hello Scala Lerner"
case 2 => "You are learning Scala"
case 3 => "Good Luck"
case _ => "Any"
}
def main(args: Array[String])
{
println(mat(
2));
}
}
OutPut: You Learning Scala

Example:3
object demo {
def main(args: Array[String]) {
println(matchTest("two"))
println(matchTest("test"))
println(matchTest(1))
println(matchTest(6))

}
def matchTest(x: Any): Any = x match {
case 1 => "one"
case "two" => 2
case y: Int => "scala.Int"
case _ => "many"
}
}
OutPut:
2
many
one
scala.Int

Pattern matching using case class
object Demo {
// Sample class
case class Person(name: String, age: Int)

def main(args: Array[String]) {
val alice = new Person("Alice", 25)
val bob = new Person("Bob", 32)
val charlie = new Person("Charlie", 32)
for (person <- List(alice, bob, charlie)) {
person match {
case Person("Alice", 25) => println("Hi Alice!")
case Person("Bob", 32) => println("Hi Bob!")
case Person(name, age) =>
println("Age: " + age + " year, name: " + name + "?")
}
}
}
}
Hi Alice!
Hi Bob!
Age: 32 year, name: Charlie?
object test {
case class emp(id: Int, employee_name: String) // case class
def main(args: Array[String]) {
val a = new emp(1, "abc")
val b = new emp(2, "xyz")
val c = new emp(3, "pgr")
for (employee <- List(a, b, c)) {
employee match {
case emp(1, "abc") => println("Hello abc")
case emp(2, "xyz") => println("Hello xyz")
case emp(id, employee_name) => println("ID: " +id + ", Employee:" + employee_name)
}
}
}
}

Scala Pattern Guards
 
We can also add conditions to the code. Simply add an if statement after the case.
object demo {
def main(args: Array[String]) {
println(mat(2))
def mat(x: Int): String = x match {
case 1 if x%2==0 => "Even"
case 1 if x%2==1 => "Odd"
case 2 if x%2==0 => "Even"
case 2 if x%2==0 => "odd"
case _ => "any"
}
}
}

object Demo {
def main(args: Array[String]) {
for (i <- 100 to 200 ) {
i match {
case a if 0 to 9 contains a => println("0-9 range: " + a)
case b if 10 to 19 contains b => println("10-19 range: " + b)
case c if 20 to 29 contains c => println("20-29 range: " + c)
case _ => println("Hmmm...")
}
}
}
}
val i = 5
i match {
  case 1 | 3 | 5 | 7 | 9 => println("odd")
  case 2 | 4 | 6 | 8 | 10 => println("even")
}
val cmd = "stop"
cmd match {
  case "start" | "go" => println("starting")
  case "stop" | "quit" | "exit" => println("stopping")
  case _ => println("doing nothing")
}
trait Command
case object Start extends Command
case object Go extends Command
case object Stop extends Command
case object Whoa extends Command

def executeCommand(cmd: Command) = cmd match {
  case Start | Go => start()
  case Stop | Whoa => stop()
}









No comments:

Post a Comment